<?php

namespace App\Http\Service\backend\Dances;

use App\Common\Utils\Common;
use App\Http\Models\Dances\Dances;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

/**
 * Class 课程管理
 * @package App\Http\Service\Dances
 */
class DancesService
{

    public function dancesList($request)
    {
        $dances = new Dances();
        $where = [];
        if (!empty($request->name)) {
            $where[] = ['name', 'like', '%' . $request->name . '%'];
        }
        if (Auth::user()['users_status'] != 1) {
            $where['teacher_id'] = Auth::user()['id'];
        }
        $dancesList = $dances::select()
            ->where($where)
            ->orderBy('updated_at', 'desc')
            ->paginate(PAGES);
        foreach ($dancesList as $key => &$val) {
            $userName = DB::table('users')->where(['id' => $val->teacher_id])->value('name');
            $val->user_name = $userName;
        }
        return $dancesList;
    }

    public function store($request)
    {
        $data = [
            'name'          => DANCE[$request->name],
            'dance_names'   => $request->name,
            'teacher_id'    => Auth::user()->users_status ? $request->teacher_id : Auth::user()->id,
            'begin_time'    => strtotime($request->begin_time),
            'end_time'      => strtotime($request->end_time),
            'course_price'  => $request->course_price,
            'vip_price'     => $request->vip_price,
            'address'       => $request->address,
            'contents'      => $request->contents,
            'league_number' => $request->league_number ?? 0,
            'status'        => $request->status,
            'types'         => Auth::user()->users_status ? $request->types : 1,
            'images'        => $request->images,
            'remark'        => $request->remark ?? ""
        ];
        //开启事物
        DB::beginTransaction();
        try {
            $res = Dances::create($data);

            if ($res->id) {
                Common::success(INSERT_SUCCES);
                DB::commit();
                return $res;
            } else {
                Common::success(INSERT_ERROR);
            }
        } catch (Exception $ex) {
            DB::rollBack();
            Common::error(INSERT_ERROR);
        }
    }

    public function updateSave($request, $id)
    {
        $dancesData = Dances::where(['id' => decryptUrl($id)])->first();
        $statusData = [];
        if ($dancesData->end_time < strtotime($request->end_time)) {
            $statusData = [
                'dance_status'            => 0,
                'league_subscribe_number' => 0,
            ];
        }
        $data = [
            'name'          => DANCE[$request->name],
            'dance_names'   => $request->name,
            'teacher_id'    => Auth::user()->users_status ? $request->teacher_id : Auth::user()->id,
            'begin_time'    => strtotime($request->begin_time),
            'end_time'      => strtotime($request->end_time),
            'course_price'  => $request->course_price,
            'vip_price'     => $request->vip_price,
            'address'       => $request->address,
            'contents'      => $request->contents,
            'status'        => $request->status,
            'league_number' => Auth::user()->users_status ? $request->types ? 0 : $request->league_number : 1,
            'types'         => Auth::user()->users_status ? $request->types : 1,
            'images'        => $request->images,
            'remark'        => $request->remark ?? ""
        ];
        $data = array_merge($statusData, $data);
        try {
            $res = Dances::where('id', decryptUrl($id))->update($data);
            if ($res) {
                Common::success(UPDATE_SUCCES);
                return $res;
            } else {
                Common::error(UPDATE_ERROR);
            }
        } catch (Exception $ex) {
            Common::error(UPDATE_ERROR);
        }
    }

    public function show($id)
    {
        $userData = DB::table('users')->where(['status' => 1, 'delete' => 1, 'users_status' => 0])->get()->toArray();
        $dancesData = Dances::where(['id' => decryptUrl($id)])->first();
        return [
            'userData'   => $userData,
            'dancesData' => $dancesData
        ];
    }

    public function remove(int $id = 0)
    {
        $dances = new Dances();
        return $dances::where('id', $id)->update(['is_delete' => 1]);
    }

}
